import { createAxios } from "@/utils/http/axios";

export interface Activity {
  id: number;
  intro: string;
  detail: string;
  sdate: string;
  edate: string;
  joiners: string;
  status: number;
  createdAt: string;
  updatedAt: string;
}

export interface CreateActivityRequest {
  intro: string;
  detail: string;
  sdate: string;
  edate: string;
  joiners: string;
  status: number;
}

export interface UpdateActivityRequest {
  intro?: string;
  detail?: string;
  sdate?: string;
  edate?: string;
  joiners?: string;
  status?: number;
}

export interface ApiResponse<T> {
  code: number;
  messages: string;
  result: T;
}

enum Api {
  Activity = '/activity',
}

// 创建专门的活动API HTTP客户端，指向本地后端服务器
const activityHttp = createAxios({
  requestOptions: {
    apiUrl: 'http://localhost:3000/api',
    joinPrefix: false,
    isTransformResponse: true,
    errorMessageMode: "message",
    withToken: false, // 活动API不需要token
  }
});

/**
 * 获取所有活动
 */
export function getActivitiesApi() {
  return activityHttp.get<ApiResponse<Activity[]>>({
    url: Api.Activity,
  });
}

/**
 * 获取单个活动
 */
export function getActivityApi(id: number) {
  return activityHttp.get<Activity>({
    url: `${Api.Activity}/${id}`,
  });
}

/**
 * 创建活动
 */
export function createActivityApi(params: CreateActivityRequest) {
  return activityHttp.post<Activity>({
    url: Api.Activity,
    data: params, // 使用data而不是params，符合RESTful规范
  });
}

/**
 * 更新活动
 */
export function updateActivityApi(id: number, params: UpdateActivityRequest) {
  return activityHttp.request<Activity>({
    url: `${Api.Activity}/${id}`,
    method: 'PATCH',
    data: params, // 使用data而不是params，符合RESTful规范
  });
}

/**
 * 删除活动
 */
export function deleteActivityApi(id: number) {
  return activityHttp.delete<Activity>({
    url: `${Api.Activity}/${id}`,
  });
} 